// pages/info/info.js
import { getUserProfile, getCode } from '../../utils/info.js'
import http from '../../api/api'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        loading: false
    },

    async login() {
        try {
            this.setData({
                loading: true
            })
            const logUser = wx.getStorageSync('user') || {};
            const logToken = wx.getStorageSync('token') || '';


            if (logUser != {} && logToken != '') {
                return wx.showToast({
                    title: '您已登录',
                    icon: 'none',
                });
            }

            const user = await getUserProfile()
            const { code } = await getCode()
            this.setData({
                userInfo: user.userInfo,
            })
            wx.setStorageSync('userInfo', user.userInfo);

            // this.getToken({ ...user, code })

            const token = await http.getWxLogin({ ...user, code })
            wx.setStorageSync('token', 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo');


            wx.navigateBack({
                delta: 1
            });

        } catch (error) {

        }
        this.setData({
            loading: false
        })
    },

    // async getToken(res) {
    //     const { encryptedData, rawData, iv, signature, code } = res
    //     const obj = { encryptedData, rawData, iv, signature, code }

    // },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})